package com.richapp.launcher.serialport.gateway;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.jack.netty.log.LoggerManager;
import com.richapp.launcher.receiver.LauncherMessageReceiver;

import java.io.File;


import android_serialport_api.SerialPort;

public class GatewayServer {

	private static final String TAG = GatewayServer.class.getName();
    private Context context;

	private static GatewayServer instance = null;
	
	private SerialPort serialPort = null;
	private GatewayReadThread sprt = null;
	private GatewayWriteThread spwt = null;
	private boolean serverRunFlag = false;
	private GatewayMessageListner messageListner = null;
	
	//最后一条写入的指令，以便重发
	private static ControlInstructionMessage lastMessage = null;
	//校对指令令牌
	private static boolean clearFlag = false;
	
	//设备名
	private String deviceName = "/dev/ttyS2";///dev/ttyS3
	//设备比特率
	private int deviceBit = 38400;
	//设备flags
	private int deviceFlag = 0;

	private GatewayServer() {
		
	}
	
	public static GatewayServer getInstance(){
		if(instance == null){
			instance = new GatewayServer();
		}
		return instance;
	}
	
	public void start() {
		try{
			if(serialPort == null){
				//初始化串口设备
				serialPort = new SerialPort(new File(deviceName), deviceBit, deviceFlag);
					//启动设备信息读取线程
				sprt = new GatewayReadThread(serialPort.getInputStream());
				sprt.setSerialPortMessgeListner(getMessageListner());
				sprt.setRunFlag(true);
				new Thread(sprt).start();
				
				//启动设备信息写入线程
				spwt = new GatewayWriteThread();
				spwt.setOs(serialPort.getOutputStream());
				spwt.setRunFlag(true);
				new Thread(spwt).start();

				Log.d(TAG, "启动网关服务");
				serverRunFlag = true;
			}
		}catch(Exception e){
			Log.d(TAG, "初始化网关串口设备失败!");
		}
	}

	public void stop() {
		if(serialPort != null){
			try{
				serverRunFlag = false;
				spwt.setRunFlag(false);
				sprt.setRunFlag(false);
				
				if(serialPort.getOutputStream() != null){
					serialPort.getOutputStream().close();
				}
				
				if(serialPort.getInputStream() != null){
					serialPort.getInputStream().close();
				}
				
				//serialPort.close();
				
				serialPort = null;
			}catch(Exception e){
				LoggerManager.getInstance().getiLogger().error(getClass(),e.getMessage());
			}
		}
	}

	public boolean isServerRun() {
		return serverRunFlag;
	}

	public void writeMessage(ControlInstructionMessage msg) {
		spwt.writeOneMessage(msg);
	}

	public GatewayMessageListner getMessageListner() {
		return messageListner;
	}

	public void setMessageListner(GatewayMessageListner messageListner) {
		this.messageListner = messageListner;
	}

	public boolean isServerRunFlag() {
		return serverRunFlag;
	}

	protected void setServerRunFlag(boolean serverRunFlag) {
		this.serverRunFlag = serverRunFlag;
	}

	public String getDeviceName() {
		return deviceName;
	}

	public void setDeviceName(String deviceName) {
		this.deviceName = deviceName;
	}

	public int getDeviceBit() {
		return deviceBit;
	}

	public void setDeviceBit(int deviceBit) {
		this.deviceBit = deviceBit;
	}

	public int getDeviceFlag() {
		return deviceFlag;
	}

	public void setDeviceFlag(int deviceFlag) {
		this.deviceFlag = deviceFlag;
	}

	public synchronized static ControlInstructionMessage getLastMessage() {
		return lastMessage;
	}

	public synchronized static void setLastMessage(ControlInstructionMessage lastMessage) {
		GatewayServer.lastMessage = lastMessage;
	}

	public static boolean isClearFlag() {
		return clearFlag;
	}

	public static void setClearFlag(boolean clearFlag) {
		GatewayServer.clearFlag = clearFlag;
	}

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }




	public void broadCastSensorStatus(String deviceName,String status){
		Intent intent = new Intent();
		intent.setAction(LauncherMessageReceiver.ACTION_SENSOR_STATUS);
		intent.putExtra("deviceName", deviceName);
		intent.putExtra("deviceStatus",status);
		context.sendBroadcast(intent);
	}

	public void broadCastSensorAlarm(String deviceName){
		Intent intent = new Intent();
		intent.setAction(LauncherMessageReceiver.ACTION_SENSOR_ALARM);
		intent.putExtra("deviceName",deviceName);
		context.sendBroadcast(intent);
	}
}
